from AiBot import WinBotMain
import time
import sys
import random
import os
import threading
import schedule

'''
短期需添加改进的功能：
1.定时运行
2.运行前检测状态，是否已经登录，是否被顶出，如果状态不正常将状态置为正常
3.多线程功能：当手动指令或定时指令发出后，检测当前状态，如果正在运行则不运行并提示，如果没有运行则运行，
如果是结束当前运行状态的指令也要能响应
4.自动进行矿点争夺
5.日常奖励
远景规划：
1.手机控制运行挂机
'''
choose=-1#全局变量，记录当前的输入，-1表示没有输入，数字和menu对应
runned=-1#全局变量，记录当前正在运行的状态，数字和menu对应

def sclick(driver,result,image):
    a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\{0}".format(image))
    if len(a)>0:
        driver.click_mouse(result, a[0].x, a[0].y, 1,True)
    time.sleep(1)

def menu():#显示菜单
    '''显示菜单'''
    while True:
        print()
        print()
        print("=====剑侠风云挂机小工具v2022.12.14.16.09=====")
        print("=========1.矿点扫荡=============")
        print("=========2.免费元宝秘籍和角色===")
        print("=========3.每日奖励=============")
        print("=========4.无限轮回塔=============")
        print("=========5.一键完成=============")
        print("=========6.重新启动=============")
        print("=========0.退出=================")
        print()
        global choose
        choose=input("请输入对应功能的数字：")
        if choose=="0":
            return

def sd():
    global choose
    choose="5"

def ds():
    schedule.every().day.at("23:00").do(sd)  
    schedule.every().day.at("07:30").do(sd)  
    schedule.every().day.at("15:00").do(sd)  
    #如果方法需要传参的话do(func,参数1)
    #开循环
    while True:
        schedule.run_pending()

def fh(result,driver):#返回主页
    '''返回主页'''
    a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\fh.png")#找到返回的坐标
    i=0
    while(len(a)!=0):
        driver.click_mouse(result, a[0].x, a[0].y, 1,True)#点击返回
        a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\fh.png")#找到返回的坐标
        time.sleep(2)
        i=i+1
        if i>4:
            raise Exception('反复返回异常，重新启动应用！')
            

def reset(result,driver):
    '''关闭窗体并重新开启'''
    sclick(driver,result,"gbct.png")

def lh(result,driver):
    '''轮回塔'''
    global choose
    global runned
    runned=choose
    choose=-1
    a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\fb.png")
    if len(a)==0:
        fh(result,driver)
    sclick(driver,result,"fb.png")
    sclick(driver,result,"fltz.png")
    sclick(driver,result,"lht.png")   
    while True:
        sclick(driver,result,"lhttz.png")
        sclick(driver,result,"lhsbqd.png")
        sclick(driver,result,"lhtlqjl.png")

def saodang(result,driver):
    '''扫荡矿点'''
    global choose
    global runned
    runned=choose
    choose=-1
    a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\fb.png")
    if len(a)==0:
        fh(result,driver)
    #点击副本
    a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\fb.png")
    driver.click_mouse(result, a[0].x, a[0].y, 1,True)
    time.sleep(2)
    #点击竞技对战
    a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\jjdz.png")
    driver.click_mouse(result, a[0].x, a[0].y, 1,True)
    time.sleep(2)
    saodang=0#统计扫荡次数
    guanggao=0#统计广告次数
    tl=0#统计领取体力次数
    #点击矿点争夺战
    a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\kdzd.png")
    driver.click_mouse(result, a[0].x, a[0].y, 1,True)
    time.sleep(2)
    while True:
        #点击扫荡
        a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\sd.png")  
        #如果矿点被抢走，提示并返回主页后退出脚本      
        if len(a)==0:
            print("==============你的矿点已经被抢走，请夺取后再运行本脚本。===============")
            break
        else:
            driver.click_mouse(result, a[0].x, a[0].y, 1,True)
            time.sleep(4)
            #如果过了两秒钟，扫荡按钮还在，说明今日的体力已经全部耗尽，退出扫荡
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\sd.png")
            if len(a)>=1:
                print()
                print("========================================================")
                print("============今日已扫荡完毕，少侠请明日再来！============")                
                break
            #判断是否装备已经满了
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\qlzb.png")
            if len(a)>=1:
                driver.click_mouse(result, a[0].x, a[0].y, 1,True)#点击清理装备
                time.sleep(2)
                #点击快速出售
                a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\kscs.png")
                driver.click_mouse(result, a[0].x, a[0].y, 1,True)
                time.sleep(2)
                #点击一键出售
                a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\yjcs.png")
                driver.click_mouse(result, a[0].x, a[0].y, 1,True)
                time.sleep(2)
                #点击返回
                a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\fh.png")
                driver.click_mouse(result, a[0].x, a[0].y, 1,True)
                time.sleep(2)
                continue
            #判断是否要领取体力
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\tlqd.png")
            if len(a)>=1:
                driver.click_mouse(result, a[0].x, a[0].y, 1,True)#点击确定
                time.sleep(2)
                a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\tlqd.png")
                if len(a)>=1:
                    print("========第%d次广告等待开始，少侠请耐心等待！======="%guanggao)
                    time.sleep(2)
                    for i in range(10):
                        time.sleep(12)
                        print("等待广告中%d"%(12-i))
                    driver.click_mouse(result, a[0].x, a[0].y, 1,True)
                #等待播放广告
                tl+=1
                print("第%d次领取体力开始，少侠请耐心等待！"%tl)
                time.sleep(35)
                #点击关闭按钮
                a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\gb.png")
                driver.click_mouse(result, a[0].x, a[0].y, 1,True)
                time.sleep(2)
                continue
            #点击双倍获取
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\sblq.png")
            driver.click_mouse(result, a[0].x, a[0].y, 1,True)
            time.sleep(2)
            #判断是否要等待广告，如果双倍获取的按钮还在，就说明需要等待广告
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\sblq.png")
            if len(a)>=1:
                guanggao+=1
                print("========第%d次广告等待开始，少侠请耐心等待！======="%guanggao)
                time.sleep(2)
                for i in range(10):
                    time.sleep(12)
                    print("等待广告中%d"%(12-i))
                driver.click_mouse(result, a[0].x, a[0].y, 1,True)#等待时间过，再次点击双倍获取按钮
            #等待播放广告        
            saodang+=1
            print("第%d次扫荡开始，少侠请耐心等待！"%saodang)
            time.sleep(35)
            #点击关闭按钮
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\gb.png")
            driver.click_mouse(result, a[0].x, a[0].y, 1,True)
            time.sleep(2)
    fh(result,driver)

def yb(result,driver):#每日免费元宝秘籍和角色
    global choose
    global runned
    runned=choose
    choose=-1
    '''每日免费元宝秘籍和角色'''
    a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\sc.png")
    if len(a)==0:
        fh(result,driver)#返回主页
    a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\sc.png")#商城
    driver.click_mouse(result, a[0].x, a[0].y, 1,True)
    time.sleep(2)
    yb,mj=0,0#表示元宝、秘籍、角色有没有被领完
    while True:
        if yb==0:
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mf.png")
            driver.click_mouse(result, a[0].x, a[0].y, 1,True)#点击免费元宝
            time.sleep(2)
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mf.png")
            if len(a)>=1:#如果界面没有变化，可能是没有机会了，也有可能是等待广告
                time.sleep(2)
                print("===========等待广告中============")
                for i in range(10):
                    time.sleep(12)
                    print("等待广告中%d"%(12-i))
                driver.click_mouse(result, a[0].x, a[0].y, 1,True)#点击免费元宝
                time.sleep(2)
                a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mf.png")
                if len(a)>=1:#等待125秒后再次点击，如果还是没有变化，那就是没有机会了
                    yb=1
                    continue
            time.sleep(32)#看广告
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\gb.png")#关闭
            driver.click_mouse(result, a[0].x, a[0].y, 1,True)
            time.sleep(2)
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\qd.png")#点击确定按钮
            driver.click_mouse(result, a[0].x, a[0].y, 1,True)
            time.sleep(2)
        elif mj==0:
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mf.png")
            driver.click_mouse(result, 130, 450, 1,True)#点击免费秘籍
            time.sleep(2)
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mf.png")
            if len(a)>=1:#如果界面没有变化，可能是没有机会了，也有可能是等待广告
                time.sleep(2)
                print("===========等待广告中============")
                for i in range(10):
                    time.sleep(12)
                    print("等待广告中%d"%(12-i))
                driver.click_mouse(result, 130, 450, 1,True)#点击免费秘籍
                time.sleep(2)
                a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mf.png")
                if len(a)>=1:#等待125秒后再次点击，如果还是没有变化，那就是没有机会了
                    mj=1
                    continue
            time.sleep(32)#看广告
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\gb.png")#关闭
            driver.click_mouse(result, a[0].x, a[0].y, 1,True)
            time.sleep(3)
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\cz.png")#看看没有有重置按钮
            if(len(a)>=1):
                driver.click_mouse(result, a[0].x, a[0].y, 1,True)
                time.sleep(2)
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\lq.png")#立即领取
            driver.click_mouse(result, a[0].x, a[0].y, 1,True)
            time.sleep(2)
        else:
            time.sleep(2)
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mf.png")
            driver.click_mouse(result, 130, 655, 1,True)#点击角色秘籍
            time.sleep(2)
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mf.png")
            if len(a)>=1:#如果界面没有变化，可能是没有机会了，也有可能是等待广告
                time.sleep(2)
                print("===========等待广告中============")
                for i in range(10):
                    time.sleep(12)
                    print("等待广告中%d"%(12-i))
                driver.click_mouse(result, 130, 665, 1,True)#点击角色秘籍
                time.sleep(2)
                a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mf.png")
                if len(a)>=1:#等待125秒后再次点击，如果还是没有变化，那就是没有机会了                    
                    fh(result,driver)
                    return
            time.sleep(32)#看广告
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\gb.png")#关闭
            driver.click_mouse(result, a[0].x, a[0].y, 1,True)
            time.sleep(3)
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\cz.png")#看看没有有重置按钮
            if(len(a)>=1):
                driver.click_mouse(result, a[0].x, a[0].y, 1,True)
                time.sleep(2)
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\lq.png")#立即领取
            driver.click_mouse(result, a[0].x, a[0].y, 1,True)

def mrjl(result,driver):#每日可领的奖励
    global choose
    global runned
    runned=choose
    choose=-1
    '''每日可领的奖励'''   
    #点击更多
    sclick(driver,result,"gd.png")
    #点击特权
    sclick(driver,result,"tq.png")
    #循环点击领取直到全部领完
    a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\tqlq.png")
    while len(a)>0:
        driver.click_mouse(result, a[0].x, a[0].y, 1,True)
        time.sleep(1)
        a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\tqqd.png")
        driver.click_mouse(result, a[0].x, a[0].y, 1,True)
        time.sleep(1)
        a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\tqlq.png")
    sclick(driver,result,"tqgb.png")
    #点击更多
    sclick(driver,result,"gd.png")
    #点击成就
    sclick(driver,result,"cj.png")
    #循环点击领取直到全部领完
    a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\cjlq.png")
    for i in range(len(a)):
        driver.click_mouse(result, a[i].x, a[i].y, 1,True)
        time.sleep(1)
        a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\cjqd.png")
        driver.click_mouse(result, a[0].x, a[0].y, 1,True)
        time.sleep(1)
    sclick(driver,result,"cjgb.png")
    #点击更多
    sclick(driver,result,"gd.png")
    #点击排行
    sclick(driver,result,"ph.png")
    #点击关卡排行
    sclick(driver,result,"gqph.png")
    #点击关卡排行领取
    sclick(driver,result,"gqphlq.png")
    sclick(driver,result,"phqd.png")
    #点击返回
    sclick(driver,result,"fh.png")
    #点击战力排行
    sclick(driver,result,"zlph.png")
    #点击战力排行领取
    sclick(driver,result,"zlphlq.png")
    sclick(driver,result,"phqd.png")
    #点击返回
    sclick(driver,result,"fh.png")
    #点击神装排行
    sclick(driver,result,"szph.png")
    #领取武器
    sclick(driver,result,"szphlq.png")
    sclick(driver,result,"phqd.png")
    #领取衣服
    sclick(driver,result,"yf.png")
    sclick(driver,result,"szphlq.png")
    sclick(driver,result,"phqd.png")
    #领取头盔
    sclick(driver,result,"tk.png")
    sclick(driver,result,"szphlq.png")
    sclick(driver,result,"phqd.png")
    #领取鞋子
    sclick(driver,result,"xz.png")
    sclick(driver,result,"szphlq.png")
    sclick(driver,result,"phqd.png")
    #领取项链
    sclick(driver,result,"xl.png")
    sclick(driver,result,"szphlq.png")
    sclick(driver,result,"phqd.png")
    #领取腰带
    sclick(driver,result,"yd.png")
    sclick(driver,result,"szphlq.png")
    sclick(driver,result,"phqd.png")
    #返回主页
    fh(result,driver)
    #每日任务
    sclick(driver,result,"mrrw.png")
    #循环点击领取直到全部领完
    a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mrrwlq.png")
    while len(a)>0:
        driver.click_mouse(result, a[0].x, a[0].y, 1,True)
        time.sleep(1)
        a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\cjqd.png")
        driver.click_mouse(result, a[0].x, a[0].y, 1,True)
        time.sleep(1)
        a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mrrwlq.png")
    a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\mrrwgb.png")
    driver.click_mouse(result, a[0].x, a[0].y, 1,True)
    time.sleep(1)
    #副本部分，包括福利挑战李的极限挑战，竞技对战里的排位赛和排名竞技场    
    sclick(driver,result,"fb.png")#点击副本    
    sclick(driver,result,"fltz.png")#点击福利挑战
    sclick(driver,result,"jxtz.png")#点击极限挑战
    sclick(driver,result,"yjsd.png")#点击一键扫荡
    sclick(driver,result,"sdlq.png")
    sclick(driver,result,"dy.png")#地域
    sclick(driver,result,"yjsd.png")#点击一键扫荡
    sclick(driver,result,"sdlq.png")
    sclick(driver,result,"em.png")#噩梦
    sclick(driver,result,"yjsd.png")#点击一键扫荡
    sclick(driver,result,"sdlq.png")    
    sclick(driver,result,"kn.png")#困难
    sclick(driver,result,"yjsd.png")#点击一键扫荡
    sclick(driver,result,"sdlq.png")    
    sclick(driver,result,"pt.png")#普通
    sclick(driver,result,"yjsd.png")#点击一键扫荡
    sclick(driver,result,"sdlq.png")
    sclick(driver,result,"jxgb.png")#关闭极限挑战
    sclick(driver,result,"fh.png")
    #除副本里的奖励外还有快速挂机没有做

def main():    
    driver = WinBotMain.build(random.randint(20000,40000))# 1. 构建实例，传入监听的端口    
    result = driver.find_window(window_name="剑侠风云")# 2. 调用 API,查询窗口句柄
    m=threading.Thread(target=menu)#显示菜单
    m.start()
    d=threading.Thread(target=ds)#启动定时任务
    d.start()
    global choose
    while choose!="0":    
        try:         
            if result is None:
                result = driver.find_window(window_name="Program Manager")
                a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\logo.png")
                driver.click_mouse(result, a[0].x, a[0].y, 7)
                time.sleep(2)
                result = driver.find_window(window_name="剑侠风云")
                if result is None:
                    choose=menu()
                    continue
            if choose=="1":
                saodang(result,driver)
            elif choose=="2":   
                yb(result,driver)  
            elif choose=="3":
                mrjl(result,driver)    
            elif choose=="4":
                lh(result,driver)  
            elif choose=="5":
                saodang(result,driver)
                yb(result,driver)    
                mrjl(result,driver)
                lh(result,driver)
            elif choose=="6":
                choose=-1
                sclick(driver,result,"gbct.png")
                result = driver.find_window(window_name="Program Manager")
                a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\logo.png")
                driver.click_mouse(result, a[0].x, a[0].y, 7)
                time.sleep(2)
                result = driver.find_window(window_name="剑侠风云")
                if result is None:
                    choose=menu()
                    continue
        except Exception as e:
            sclick(driver,result,"gbct.png")
            result = driver.find_window(window_name="Program Manager")
            a=driver.find_images(result, __file__.replace(os.path.basename(__file__),"")+r"images\logo.png")
            driver.click_mouse(result, a[0].x, a[0].y, 7)
            time.sleep(2)
            result = driver.find_window(window_name="剑侠风云")
            choose=runned


if __name__ == '__main__':
    # 执行脚本
    main()
